from flask import Flask
from .extensions import db, scheduler
from .config import Config

def create_app(config_class = Config):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_object(config_class)

    #测试请求第三方接口
    @app.route('/test_api', methods=['GET'])
    def test_api():
        from..services.api_client import APIClient
        client = APIClient()
        try:
            token = client.get_token()
            return {'message': 'API请求成功', 'token': token}, 200
        except Exception as e:
            return {'message': 'API请求失败', 'error': str(e)}, 500

    #初始化扩展
    # initialize_extensions(app)

    #注册蓝图 
    # register_blueprints(app)

    #初始化定时任务
    # with app.app_context():
    #     from .tasks import scheduled_tasks
    #     scheduled_tasks.start()
    return app
def initialize_extensions(app):
    db.init_app(app)
    scheduler.init_app(app)
    scheduler.start()

def register_blueprints(app):
    from .routes.inventory_bp import inventory_bp

    app.register_blueprint(inventory_bp, url_prefix='/api/inventory')